package com.example.benyq.mediaplayerdemo;

import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.util.Log;

import java.io.FileDescriptor;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static com.example.benyq.mediaplayerdemo.PlayState.COMPLETE;
import static com.example.benyq.mediaplayerdemo.PlayState.PLAY_LAST;
import static com.example.benyq.mediaplayerdemo.PlayState.PLAY_NEXT;

/**
 * @author benyq
 * @date 17-12-11 18:27
 * @E-mail： 1520063035@qq.com
 * @description
 */
public class Player implements IPlayback,MediaPlayer.OnCompletionListener,MediaPlayer.OnBufferingUpdateListener,MediaPlayer.OnErrorListener,MediaPlayer.OnPreparedListener{

    public static Player sInstance = null;
    private MediaPlayer mMediaPlayer;
    private boolean isPaused;
    private List<Callback> mCallbacks = new ArrayList<>(2);
    private String song;
    private int currentPosition = 0;

    public Player() {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setOnCompletionListener(this);
        mMediaPlayer.setOnBufferingUpdateListener(this);
        mMediaPlayer.setOnPreparedListener(this);
    }

    public static Player getInstance(){

        if (sInstance == null){
            synchronized (Player.class){
                if (sInstance == null){
                    sInstance = new Player();
                }
            }
        }
        return sInstance;

    }


    private void notifyPlayStatusChanged(PlayState status) {
        for (Callback callback : mCallbacks) {
            callback.onPlayStatusChanged(status);
        }
    }
    private void notifyComplete(PlayState state) {
        for (Callback callback : mCallbacks) {
            callback.onComplete(state);
        }
    }

    @Override
    public void onBufferingUpdate(MediaPlayer mediaPlayer, int i) {

    }

    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {
        mMediaPlayer.reset();
    }

    @Override
    public boolean onError(MediaPlayer mediaPlayer, int i, int i1) {
        notifyPlayStatusChanged(PlayState.ERROR);
        return false;
    }

    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
        mMediaPlayer.start();
        seekTo(currentPosition);
        Log.e("onPrepared", "ss"+currentPosition);
        notifyPlayStatusChanged(PlayState.PLAYING);
    }

    @Override
    public boolean play() {
        if (isPaused){
            mMediaPlayer.start();
            notifyPlayStatusChanged(PlayState.PLAYING);
            return true;
        }
        return false;
    }


    @Override
    public boolean play(String song) {
        try {
            mMediaPlayer.reset();
            mMediaPlayer.setDataSource(song);
            mMediaPlayer.prepare();
            this.song = song;
            currentPosition = 0;
            notifyPlayStatusChanged(PlayState.PLAYING);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            notifyPlayStatusChanged(PlayState.ERROR);
        }
        return false;
    }

    @Override
    public boolean pause() {
        if (mMediaPlayer.isPlaying()){
            mMediaPlayer.pause();
            isPaused = true;
            notifyPlayStatusChanged(PlayState.PAUSE);
            return true;
        }
        return false;
    }

    @Override
    public boolean isPlaying() {
        return mMediaPlayer.isPlaying();
    }

    @Override
    public int getProgress() {
        currentPosition = mMediaPlayer.getCurrentPosition();
        if (currentPosition >= getDuration()){
            notifyPlayStatusChanged(COMPLETE);
        }
        return currentPosition;
    }

    @Override
    public int getDuration() {
        return mMediaPlayer.getDuration();
    }

    @Override
    public void playNext() {
        notifyPlayStatusChanged(PLAY_NEXT);
    }

    @Override
    public void playLast() {
        notifyPlayStatusChanged(PLAY_LAST);
    }

    @Override
    public boolean seekTo(int progress) {
        try {
            mMediaPlayer.seekTo(progress);
        }catch (IllegalStateException e){
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public void registerCallback(Callback callback) {
        mCallbacks.add(callback);
    }

    @Override
    public void unregisterCallback(Callback callback) {
        mCallbacks.remove(callback);
    }

    @Override
    public void removeCallbacks() {
        mCallbacks.clear();
    }

    @Override
    public void releasePlayer() {
        mMediaPlayer.reset();
        mMediaPlayer.release();
        mMediaPlayer = null;
        sInstance = null;
        song = null;
    }

}
